package com.fyzk.activity.prize;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.shuzicai.klpk.KLPKPopupItemBean;
import com.fyzk.activity.lottery.shuzicai.klpk.KLPKPopupwindow;
import com.fyzk.activity.user.NetProgressView;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.DownMenuPopupwindow;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import http.Common.CommandUtil;
import http.DataUtil.text.TextUtil;
import http.StringResultBean;

@ContentView(R.layout.ac_beidan_prize)
public class BeiDanPrizeActivity extends BaseActivity implements DownMenuPopupwindow.DownMenuPopupItemListener,
		KLPKPopupwindow.KLPKPopuponItemListener{

	@ViewInject(R.id.beidan_prize_topview)
	private TopTitleView topView;
	@ViewInject(R.id.beidan_prize_listview)
	private ListView listView;
	private List<BeiDanPrizeBean.RacesBean> infors = new ArrayList<>();
	private BeiDanPrizeAdapter adapter;
	@ViewInject(R.id.beidan_prize_netview)
	private NetProgressView netProgressView;

	private String phase = "";
	private String[] phases;

	private DownMenuPopupwindow downMenu;

	private int popupId = 0;
	private int[] popup_itemsId = {R.string.beidan_rspf, R.string.beidan_bf, R.string
			.beidan_zjq, R.string.beidan_bqc, R.string.beidan_sxds};
	protected KLPKPopupwindow top_popup;
	protected List<KLPKPopupItemBean> popup_items = new ArrayList<>();

	private int lotteryId = LotteryIDUtils.DCSPF_LOTTERY_ID_INT;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		findBeiDanResults();
	}

	private void initView() {

		topView.setOnTitleClickListener(this);
		topView.setJianTouVisible(View.VISIBLE);
		topView.setRightTxt("选择期号");

		for (int i = 0; i < popup_itemsId.length; i++) {

			KLPKPopupItemBean itemBean = new KLPKPopupItemBean();
			itemBean.str1 = getString(popup_itemsId[i]);
			popup_items.add(itemBean);
		}
		top_popup = new KLPKPopupwindow(this, AbsListView.LayoutParams.MATCH_PARENT, AbsListView
				.LayoutParams.WRAP_CONTENT,
				popup_items, popup_items.size());
		top_popup.setHm_PopupOnItemListener(this);

		topView.setCenterText(popup_items.get(popupId).str1);
		top_popup.setCheckItem(popupId);

		adapter = new BeiDanPrizeAdapter(getContext(), infors, lotteryId);
		listView.setAdapter(adapter);

	}

	private void findBeiDanResults(){
		JSONObject obj = new JSONObject();
		try {
			obj.put("lotteryType", lotteryId);
			obj.put("phase",phase);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		httpPostJson(CommandUtil.FINDDCRESULTS, Commands.FINDDCRESULTS,obj,true);
	}

	@Override
	protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
		super.onSuccess(requestCode, resultBean, result);
		netProgressView.dismiss();
		BeiDanPrizeBean response = JSON.parseObject(resultBean.getData(),BeiDanPrizeBean.class);
		if (TextUtil.isEmpty(phase)) {
			String phaseStr = response.getPhases();
			if (phaseStr.contains(";")) {
				phases = phaseStr.split(";");
			}
			phase = phases[0];
			downMenu = new DownMenuPopupwindow(this,
					AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT,
					phases);
			downMenu.setDownMenuPopupOnItemListener(this);
			topView.setRightTxt("第"+phase+"期");
		}
		infors = response.getRaces();
		adapter.setInfors(infors, lotteryId);
	}

	@Override
	protected void onFailure(int requestCode, String errorCode,
							 String message) {
		if(errorCode.equals("20004")){
			netProgressView.noneRecord(message);
		}else{
			netProgressView.fail(message);
		}
	}

	@Override
	public void onRightClick() {
		if (downMenu != null) {
			downMenu.showPopupWindow(topView);
		} else {
			AppToast.getToast().show("暂无新期");
		}
	}

	@Override
	public void onCenterClick() {
		top_popup.showPopupWindow(topView);
	}

	@Override
	public void onMenuItem(int position) {
		phase = phases[position];
		topView.setRightTxt("第"+phase+"期");
		findBeiDanResults();
	}

	@Override
	public void menuShow() {

	}

	@Override
	public void menuDismiss() {

	}

	@Override
	public void onItem(int position) {
		popupId = position;
		topView.setCenterText(popup_items.get(popupId).str1);
		switch (position) {
			case 0:
				lotteryId = LotteryIDUtils.DCSPF_LOTTERY_ID_INT;
				break;
			case 1:
				lotteryId = LotteryIDUtils.DCBF_LOTTERY_ID_INT;
				break;
			case 2:
				lotteryId = LotteryIDUtils.DCZJQ_LOTTERY_ID_INT;
				break;
			case 3:
				lotteryId = LotteryIDUtils.DCBQC_LOTTERY_ID_INT;
				break;
			case 4:
				lotteryId = LotteryIDUtils.DCSXDS_LOTTERY_ID_INT;
				break;
		}
		findBeiDanResults();
	}

	@Override
	public void popupShow() {
		topView.setJianTouRotate_shun();
	}

	@Override
	public void popupDismiss() {
		topView.setJianTouRotate_ni();
	}
}
